
function [beta_hat L]=est_cauchit(y,x);

%{
This routine runs a logit regression of y on x (based on a linear index). 
y is an (n x 1) vector consisting of zeros and ones, while x is a (n x k) 
matrix of explanatory variables. 

The output is a (1 x k) column vector of estimated logit coefficients and 
the value of the maximizied log-likelihood.
%}

dim=size(x);
k=dim(2); %number of variables in x

beta_ini=zeros(k,1); 
options=optimset('Display', 'off'); 
beta_ini=fsolve(@(beta) logit_score(beta, y, x),  beta_ini, options); %logit starting value (logit is very robust)

beta_hat=fsolve(@(beta) cauchit_score(beta, y, x),  beta_ini, options); 

%Note: the command @(beta) logit_score(beta, y, x) designates logit_score
%as a function of the single argument beta for use with the fsolve command. 
%(Formally, it creates an anonymous function.) Thus, beta_ini is the 
%initial value fsolve will use to search for the zeros of the score fn.

%Log-likelihood:
L=sum( log( tcdf(x*beta_hat,1) ).*(y==1) + log( tcdf(-x*beta_hat, 1)).*(y==0) );
beta_hat=beta_hat'; %1 x k

%Calculate the asymptotic covariance matrix
 



